<template>
  <div class="closeAccountNews">
    <u-nav-bar :title="'注销账户'" />
    <div class="closeAccountNews__ct">
      <div class="title">发送短信验证</div>
      <div class="memo">请输入我们给您发送的短信验证码以帮助我们确认您的身份</div>
      <van-form @submit="onSubmit">
        <van-field v-model="phone" label-width="0" diabled name="手机号" clearable placeholder="手机号"></van-field>
        <van-field
          v-model="formModel.code"
          label-width="30"
          name="验证码"
          maxlength="6"
          placeholder="验证码"
          :rules="[{ required: true, message: '请填写验证码' }]"
        >
          <template #button>
            <div
              :class="typeof verification === 'string' ? 'verification__class' : ''"
              style="color: #409eff"
              @click="getMobileValiCode(userInfo.account, verification)"
            >
              {{ verification }}
            </div>
          </template>
        </van-field>
        <van-button native-type="submit" :class="submitStatus === true ? 'button__active' : ''">
          我已发送短信，请验证
        </van-button>
      </van-form>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, reactive, watchEffect } from 'vue'
import { Toast } from 'vant'
import { useRouter } from 'vue-router'
import uNavBar from '@/components/NavBar.vue'
import store from '@/store/index'

export default defineComponent({
  name: 'CloseAccountNews',
  components: {
    'u-nav-bar': uNavBar
  },

  // 只注重主流程的执行，不同业务的函数拆分出去，业务逻辑清晰明了
  setup() {
    const userInfo = store.getters['user/getUserInfo']
    const phone = userInfo?.consignerContact?.contactPhone?.replace(/([\s\S]{3})([\s\S]{4})/, '$1****') ?? ''
    const verification = ref<any>('获取验证码')
    const smsTimer = ref<any>(null)
    const formModel = reactive<{
      code: string
    }>({
      code: ''
    })

    const submitStatus = ref<boolean>(false)

    const router = useRouter()

    watchEffect(() => {
      if (formModel.code && formModel.code.length === 6) {
        submitStatus.value = true
      } else {
        submitStatus.value = false
      }
    })
    /** 事件专区 */
    const onSubmit = async (): Promise<void> => {
      const result = await store.dispatch('user/handleAccountDestroy', formModel)
      if (result) {
        Toast.success('申请成功')
        router.go(-2)
      }
    }

    const handleGetSms = async (): Promise<boolean> => {
      let finalResult = false
      try {
        const result = await store.dispatch('user/handleAccountDestroySms')

        if (result) {
          finalResult = true
          Toast.success('发送成功')
        }
      } catch (e) {
        //
      }
      return finalResult
    }

    const getMobileValiCode = (mobile: number | string, valiCode: string | number): void => {
      smsTimer.value && clearTimeout(smsTimer.value)

      try {
        if (Number(verification.value) && Number(verification.value) > 0) {
          return
        }
        smsTimer.value = setTimeout(async () => {
          const result = await handleGetSms()
          if (!result) return
          if (typeof valiCode === 'string') {
            let timeInt: any = null
            verification.value = 60
            timeInt && clearInterval(timeInt)
            timeInt = setInterval(() => {
              if (verification.value > 0 && typeof verification.value !== 'string') {
                verification.value -= 1
              } else {
                verification.value = '获取验证码'
                timeInt && clearInterval(timeInt)
                timeInt = null
              }
            }, 1000)
          }
        }, 300)
      } catch (e) {
        //
      }
    }

    return {
      onSubmit,
      phone,
      verification,
      userInfo,
      getMobileValiCode,
      formModel,
      submitStatus
    }
  }
})
</script>
<style lang="scss" scoped>
@import '@/style/variables.scss';
@import '@/style/mixins.scss';
.closeAccountNews {
  @include flex-cloumn-width-height-100;
  background-color: $background-color-base;
  &__ct {
    @include flex-scroll-content-pd(24px 12px 0 12px);
    position: relative;

    .van-cell {
      margin-bottom: 10px;
      background: #ffffff;
      border-radius: 6px;
      line-height: 34px;
      box-sizing: content-box;
      padding: 12px;
      width: calc(100% - 24px);
      input {
        background-color: white !important;
      }
    }
    .van-cell::after {
      border: unset;
    }
    .title {
      font-size: 18px;
      font-weight: 500;
      color: #101736;
      line-height: 25px;
      margin: 20px 0 10px 0;
    }
    .memo {
      font-size: 12px;
      font-weight: 400;
      color: #575c72;
      line-height: 22px;
      margin-bottom: 42px;
    }
    .box {
      box-sizing: border-box;
      width: 100%;
      height: 54px;
      position: relative;
      background: white;
      border: 1px solid #ededed;
      border-radius: 4px;
      margin-bottom: 30px;
      .fix__text {
        font-size: 12px;
        font-weight: 400;
        color: #575c72;
        line-height: 12px;
        position: absolute;
        left: 28px;
        top: -6px;
      }
      .text {
        font-size: 18px;
        font-weight: 500;
        color: #101736;
        line-height: 54px;
        padding-left: 23px;
      }
      .text__first {
        padding-left: 45px;
      }
    }
    button {
      width: 325px;
      height: 48px;
      background: linear-gradient(125deg, #74cdff 0%, #409eff 100%);
      box-shadow: 0px 3px 12px 0px rgba(64, 158, 255, 0.4);
      border-radius: 24px;
      color: white;
      font-size: 16px;
      margin: 20px auto 12px auto;
      display: block;
      border: none;
      position: absolute;
      bottom: 10px;
      left: 50%;
      margin-left: -162.5px;
      opacity: 0.5;
    }
    .button__active {
      opacity: 1;
    }
    .verification__class {
      width: 84px;
      height: 28px;
      background: rgba(64, 158, 255, 0.1);
      line-height: 28px;
      text-align: center;
      border-radius: 15px;
      border: 1px solid rgba(64, 158, 255, 0.5);
      font-size: 12px;
    }
  }
}
</style>
